//
//  CreateTaskSelectPersonViewController.m
//  MyProject
//
//  Created by Pei on 16/8/6.
//  Copyright © 2016年 Xiaobo Pei. All rights reserved.
//

#import "CreateTaskSelectPersonViewController.h"
#import "UserInfo.h"

@interface CreateTaskSelectPersonViewController ()<UITableViewDataSource, UITableViewDelegate>
{
    UITableView *_firstTable;
    UITableView *_secondTable;
    
    NSArray *_firstDataArray;
    NSArray *_secondDataArray;
    
    NSIndexPath *_lastIndexPath1;
    NSIndexPath *_lastIndexPath2;
    
    NSString *_currentAction;
    
    NSDictionary *superDic;
    
    NSDictionary *firstDic;
    NSDictionary *secondDic;
    
    NSArray *peiTongAry;
}

@end

@implementation CreateTaskSelectPersonViewController

- (id)initWithData:(NSArray*)personA super:(NSDictionary*)dic{
    self = [super init];
    if (self) {
        peiTongAry = personA;
        
        NSMutableArray *hhhhh = [[NSMutableArray alloc] initWithCapacity:3];
        [hhhhh removeAllObjects];
        for (NSDictionary *nary in peiTongAry) {
            NSMutableDictionary * tempDDD = [[NSMutableDictionary alloc] initWithDictionary:nary];
            [tempDDD setValue:@"1" forKey:@"isSelect"];
            [hhhhh addObject:tempDDD];
        }
        
        peiTongAry = hhhhh;
        
    }
    return self;
}

- (void)viewDidLoad {
    
    self.navTitle = @"选择";
    
    [super viewDidLoad];
    
    _lastIndexPath1 = nil;
    _lastIndexPath2 = nil;
    
    UIButton *rightBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 22)];
    [rightBtn setTitle:@"确认" forState:UIControlStateNormal];
    [rightBtn setTitleColor:kBlueColor forState:UIControlStateNormal];
    rightBtn.titleLabel.font = kAppFont(Home_Big_Size);
    [rightBtn addTarget:self action:@selector(rightBtnAcion) forControlEvents:UIControlEventTouchUpInside];
    [self.navigationItem setRightBarButtonItem:[[UIBarButtonItem alloc] initWithCustomView:rightBtn]];
    
    _firstTable = [[UITableView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, kDeviceWidth/2.0f, kDeviceHeight-NAV_HEIGHT)];
    _firstTable.delegate = self;
    _firstTable.dataSource = self;
    _firstTable.tag = 116;
    [self.view addSubview:_firstTable];
    
    UIView *lineView = [[UIView alloc] init];
    lineView.frame = CGRectMake(kDeviceWidth/2.0f, 0.0f, 0.5f, kDeviceHeight-NAV_HEIGHT);
    lineView.backgroundColor = kGreenColor;
    [self.view addSubview:lineView];
    
    
    _secondTable = [[UITableView alloc] initWithFrame:CGRectMake(kDeviceWidth/2.0f+0.5f, 0.0f, kDeviceWidth/2.0f-0.5f, kDeviceHeight-NAV_HEIGHT)];
    _secondTable.delegate = self;
    _secondTable.dataSource = self;
    _secondTable.tag = 118;
    [self.view addSubview:_secondTable];
    
    
    
    [self getCompanyListData];
}

- (void)rightBtnAcion{
    NSMutableArray *tttttt = [[NSMutableArray alloc] initWithCapacity:3];
    [tttttt removeAllObjects];
    for (int i = 0; i < _secondDataArray.count; i ++) {
        NSDictionary *rrrr = [_secondDataArray objectAtIndex:i];
        //NSMutableDictionary *addSuperDic = [[NSMutableDictionary alloc] initWithDictionary:rrrr];
        if ([[rrrr valueForKey:@"isSelect"] intValue] == 1) {
            //[addSuperDic setObject:superDic forKey:@"superDic"];
            [tttttt addObject:rrrr];
        }
        //[aaaa setValue:@"0" forKey:@"isSelect"];
        
    }
    if (tttttt != nil && tttttt.count > 0) {
        if ([self.delegate respondsToSelector:@selector(passData:superDic:yuanPerson:)]) {
            [self.delegate passData:tttttt superDic:superDic yuanPerson:peiTongAry];
            [self.navigationController popViewControllerAnimated:YES];
        }
    }else{
        if ([self.delegate respondsToSelector:@selector(passData:superDic:yuanPerson:)]) {
            [self.delegate passData:tttttt superDic:superDic yuanPerson:peiTongAry];
            [self.navigationController popViewControllerAnimated:YES];
        }
    }
}



- (void)getCompanyListData{
    //Get_AllOrgs
    [[YMNetLib getInstance] connectWithAction:Get_AllOrgs method:@"GET" withContent:@"" withBody:nil withDelegate:self];
    _currentAction = Get_AllOrgs;
}

- (void)getUserListByKeyWordListData:(NSString*)OrgCode{
    //Get_UserListByKeyWord  YFZX
    [[YMNetLib getInstance] connectWithAction:Get_UserListByKeyWord method:@"GET" withContent:[NSString stringWithFormat:@"keyword=%@&UserRole=%@&DeptCode=%@", @"", @"Inspect", OrgCode] withBody:nil withDelegate:self];
    _currentAction = Get_UserListByKeyWord;
}

- (void)NetDoOverSuccess:(NSDictionary*)body
{
    if ([_currentAction isEqualToString:Get_AllOrgs]) {
        if ([[YMNetDataUtil getSuccessState:body] intValue] == 1){
            _firstDataArray = [body valueForKey:@"OrgList"];
            firstDic = body;
            
            NSMutableArray *dddd = [[NSMutableArray alloc] initWithCapacity:3];
            [dddd removeAllObjects];
            for (NSDictionary *ttt in _firstDataArray) {
                if ([[ttt valueForKey:@"OrgLevel"] intValue] == 0) {
                    [dddd addObject:ttt];
                }
            }
            _firstDataArray = dddd;
            [_firstTable reloadData];
            
            if (_firstDataArray != nil && _firstDataArray.count >0) {
                NSDictionary *aaaaa = [_firstDataArray objectAtIndex:0];
                superDic = aaaaa;
                [self getUserListByKeyWordListData:[aaaaa objectForKey:@"OrgCode"]];
            }
        }
    }else if ([_currentAction isEqualToString:Get_Important_Working_List]){
        
    }
    else{
        if ([[YMNetDataUtil getSuccessState:body] intValue] == 1){
            _secondDataArray = [body valueForKey:@"UserList"];
            secondDic = body;
            [_secondTable reloadData];
        }
    }
    
}



- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (tableView.tag == 116) {
        id tempId = [firstDic objectForKey:@"OrgList"];
        if ([tempId isKindOfClass:[NSString class]]) {
            if ([tempId isEqualToString:@""]) {
                return 0;
            }
        }
        return _firstDataArray.count;
    }else{
        id tempId = [secondDic objectForKey:@"UserList"];
        if ([tempId isKindOfClass:[NSString class]]) {
            if ([tempId isEqualToString:@""]) {
                return 0;
            }
        }
        return _secondDataArray.count;
    }
    return 0;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView.tag == 116) {
        static NSString *identifier = @"first_cell";
        UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:identifier];
        if (cell == nil)
        {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        }
        cell.textLabel.textColor = kRGB(163, 163, 163);
        cell.textLabel.font = kAppFont(Home_Small_Size+2);
        //cell.selectionStyle = UITableViewCellSelectionStyleDefault;
        //cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        UIView *bgColorView = [[UIView alloc] init];
        bgColorView.backgroundColor = kRGB(226, 226, 226);
        bgColorView.layer.masksToBounds = YES;
        cell.selectedBackgroundView = bgColorView;
        
        NSDictionary *tempDic = [_firstDataArray objectAtIndex:indexPath.row];
        
        cell.textLabel.text = [tempDic valueForKey:@"OrgName"];
        
        if (indexPath.row == 0) {
            [tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
        }
        
        return cell;
    }else{
        static NSString *identifier = @"second_cell";
        UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:identifier];
        if (cell == nil)
        {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        }
        cell.textLabel.textColor = kRGB(163, 163, 163);
        cell.textLabel.font = kAppFont(Home_Small_Size+2);
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        NSDictionary *tempDic = [_secondDataArray objectAtIndex:indexPath.row];
        
        if (peiTongAry != nil && peiTongAry.count > 0) {
            
            BOOL isExit = NO;
            NSDictionary *currentPerson;
            for (NSDictionary *nary in peiTongAry) {
                if ([[nary valueForKey:@"UserID"] isEqualToString:[tempDic valueForKey:@"UserID"]]) {
                    isExit = YES;
                    currentPerson = nary;
                }
            }
            
            if (isExit && [[currentPerson valueForKey:@"isSelect"] intValue] == 1) {
                tempDic = currentPerson;
                
                NSMutableArray *tttttt = [[NSMutableArray alloc] initWithArray:_secondDataArray];
                for (int i = 0; i < _secondDataArray.count; i ++) {
                    NSDictionary *rrrr = [_secondDataArray objectAtIndex:i];
                    if ([[rrrr valueForKey:@"UserID"] isEqualToString:[tempDic valueForKey:@"UserID"]]){
                        [tttttt replaceObjectAtIndex:i withObject:tempDic];
                    }
                }
                    _secondDataArray = tttttt;
            }
            
            if ([[tempDic valueForKey:@"isSelect"] intValue] == 0) {
                cell.accessoryType = UITableViewCellAccessoryNone;
            }else{
                cell.accessoryType = UITableViewCellAccessoryCheckmark;
            }
            

        }else{
            if ([[tempDic valueForKey:@"isSelect"] intValue] == 0) {
                cell.accessoryType = UITableViewCellAccessoryNone;
            }else{
                cell.accessoryType = UITableViewCellAccessoryCheckmark;
            }
        }
        
        

        
        cell.textLabel.text = [tempDic valueForKey:@"UserName"];
        return cell;
    }
    return nil;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 54.0f;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView.tag == 116){
        NSDictionary *tempDic = [_firstDataArray objectAtIndex:indexPath.row];
        superDic = tempDic;
        [self getUserListByKeyWordListData:[tempDic objectForKey:@"OrgCode"]];
    }else{
        NSDictionary *tempDic = [_secondDataArray objectAtIndex:indexPath.row];
        
        if ([[tempDic valueForKey:@"isSelect"] intValue] == 0) {
            UITableViewCell *newCell = [tableView cellForRowAtIndexPath:
                                        indexPath];
            newCell.accessoryType = UITableViewCellAccessoryCheckmark;
            
            NSMutableDictionary *aaaa = [[NSMutableDictionary alloc] initWithDictionary:tempDic];
            [aaaa setValue:@"1" forKey:@"isSelect"];
            
            NSMutableArray *tttttt = [[NSMutableArray alloc] initWithArray:_secondDataArray];
            for (int i = 0; i < _secondDataArray.count; i ++) {
                NSDictionary *rrrr = [_secondDataArray objectAtIndex:i];
                if ([[rrrr valueForKey:@"UserID"] isEqualToString:[aaaa valueForKey:@"UserID"]]){
                    [tttttt replaceObjectAtIndex:i withObject:aaaa];
                }
            }
            
            _secondDataArray = tttttt;
            
        }else{
            UITableViewCell *newCell = [tableView cellForRowAtIndexPath:
                                        indexPath];
            newCell.accessoryType = UITableViewCellAccessoryNone;
            
            NSMutableDictionary *aaaa = [[NSMutableDictionary alloc] initWithDictionary:tempDic];
            [aaaa setValue:@"0" forKey:@"isSelect"];
            
            NSMutableArray *tttttt = [[NSMutableArray alloc] initWithArray:_secondDataArray];
            for (int i = 0; i < _secondDataArray.count; i ++) {
                NSDictionary *rrrr = [_secondDataArray objectAtIndex:i];
                if ([[rrrr valueForKey:@"UserID"] isEqualToString:[aaaa valueForKey:@"UserID"]]) {
                    [tttttt replaceObjectAtIndex:i withObject:aaaa];
                }
            }
            
            NSMutableArray *personADD = [[NSMutableArray alloc] initWithArray:peiTongAry];
            for (int k = 0; k < peiTongAry.count; k ++) {
                NSDictionary *rrrr = [peiTongAry objectAtIndex:k];
                if ([[rrrr valueForKey:@"UserID"] isEqualToString:[aaaa valueForKey:@"UserID"]]) {
                    [personADD replaceObjectAtIndex:k withObject:aaaa];
                }
            }
            peiTongAry = personADD;
            
            _secondDataArray = tttttt;
        }
        [_secondTable reloadData];
    }
    
}

@end
